package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-02-19
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> treeSelect(Long parentId) {
        //根据parentId查找子节点
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, parentId);
        List<Category> categories = baseMapper.selectList(lambdaQueryWrapper);

        if (!CollectionUtils.isEmpty(categories)){
            //查找当前节点是否有子节点
            categories.forEach(category -> {
                Long count = baseMapper.selectCount(new LambdaQueryWrapper<Category>()
                        .eq(Category::getParentId, category.getId()));
                category.setHasChildren(count > 0);
            });
        }
        return categories;
    }

    @Override
    public List<CategoryVo> getLevelOneCategory() {
        //查询所有一级分类
        List<Category> allCategoryList = baseMapper.selectList(
                new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0)
        );
        //转换为vo
        return allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<CategoryVo> tree() {
        //查询所有分类
        List<Category> allCategoryList = baseMapper.selectList(null);
        //转换为vo
        List<CategoryVo> categoryVoList = allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
        return buildTree(categoryVoList);
    }

    /**
     *使用递归方法建树
     */
    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        //定义map存放分类数据
        HashMap<Long, CategoryVo> longCategoryVoHashMap = new HashMap<>();
        categoryVoList.forEach(item -> {
            longCategoryVoHashMap.put(item.getId(), item);
        });
        //存放一级分类
        List<CategoryVo> voList = new ArrayList<>();
        //遍历所有分类，找到父节点为0的节点，就是一级节点
        for (CategoryVo categoryVo : categoryVoList) {
            if (categoryVo.getParentId().intValue() == 0){
                voList.add(categoryVo);
            }else {
                //子节点，找到父节点，然后添加
                Long currentParentId = categoryVo.getParentId();
                CategoryVo parentCategoryVo = longCategoryVoHashMap.get(currentParentId);
                parentCategoryVo.getChildren().add(categoryVo);
            }
        }
        return voList;
    }

}
